using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using WuLibrary;

namespace TowerDefence
{
    public class UI_Battle : UILayer
    {
        private Text mLifeText;
        private GameObject mObjTowerContent;
        private GameObject mEnemyHpInfo;

        private Text mWaveText;
        
        private Text mWaveTimeText;
        
        public GameObject mBaseTowerButton;
        
        private List<TowerInfo> mTowerInfoList = new List<TowerInfo>();
        
        private List<GameObject>mTowerButtonList = new List<GameObject>();
        
        private void Update()
        {
            BattleManager.BattleUpdate();
            
            if (BattleManager.mBattleStop==false&&Input.GetMouseButtonDown(0))
            {
                if (mObjTowerContent.activeSelf)
                {
                    if (EventSystem.current.IsPointerOverGameObject()==false)
                    {
                        {
                            mObjTowerContent.SetActive(false);
                        }
                    }
                }
                     
                else
                {
                    
                    Vector3 pos=Input.mousePosition;
                    pos.x -= Screen.width >> 1;
                    pos.y -= Screen.height >> 1;
                    
                    float scale=Screen.height/1080.0f;
                    pos.x/=scale;
                    pos.y/=scale;

                    Vector3 posBtn = pos;
                  
                    RectTransform rect = GetComponent<RectTransform>();
                    pos.x/=rect.rect.width;
                    pos.y/=rect.rect.height;
                    pos.x += 0.5f;
                    pos.y += 0.5f;
                    Ray ray= Camera.main.ViewportPointToRay(pos);
                    RaycastHit[] hits = Physics.RaycastAll(ray);
                    for (int i = 0; i < hits.Length; i++)
                    {
                        if (hits[i].collider.name == "map_show")
                        {
                            pos=hits[i].point;
                            pos.x = Mathf.RoundToInt(pos.x);
                            pos.z = Mathf.RoundToInt(pos.z);

                            if (BattleManager.IsClickTower(pos) == false)
                            {
                                mObjTowerContent.transform.localPosition = posBtn;
                                mObjTowerContent.SetActive(true);
                                return;
                            }
                        }
                    }
                    ColorDebugTools.Log("点击到防御塔",LogColor.Red);
                  

                }
               
            }

        
        }

        public void BattleStop()
        {
            mObjTowerContent.SetActive(false);
        }

        public void InitData(string mapName,List<TowerInfo> towerInfoList)
        {
            mTowerInfoList=towerInfoList;
            AudioManager.PlayBg("music_fuben");
            MapManager.Map.transform.localPosition=Vector3.zero; 
            //  BattleManager.MapPathList = MapManager.GetMapPath("1");
            // Enemy enemy= EnemyManager.CreateEnemy("Warrior", BattleManager.MapPathList);
            BattleManager.InitData( this,MapManager.GetMapPath(mapName));
            RefershLife();
            mObjTowerContent.SetActive(false);
            
            
            mWaveTimeText.text = "";
            mWaveText.text = "";

            for (int i = 0; i < mTowerInfoList.Count; i++)
            {
                var obj=GameObject.Instantiate(mBaseTowerButton, mObjTowerContent.transform);
                obj.gameObject.SetActive(true);
                obj.name = "TowerBtn" + (i + 1).ToString();
                mTowerButtonList.Add(obj);
                var btn=obj.GetComponent<Button>();
                btn.onClick.AddListener(() =>
                {
                    OnButtonClick("TowerBtn",obj);
                });
                var text= obj.GetComponentInChildren<Text>();
                text.text = mTowerInfoList[i].TowerName;
            }
        }

        public override void OnNodeLoad()
        {
            MapManager.Map.transform.localPosition=Vector3.zero; 
           //  BattleManager.MapPathList = MapManager.GetMapPath("1");
           // Enemy enemy= EnemyManager.CreateEnemy("Warrior", BattleManager.MapPathList);
         
            RefershLife();
            mObjTowerContent.SetActive(false);

        }

        public override void OnEnter()
        {
           
        }

        public override void OnExit()
        {
           
        }

        public void BindEnemy(Enemy enemy)
        {
            GameObject obj = Instantiate(mEnemyHpInfo,transform);
           
            obj.SetActive(true);
            EnemyHpItem hpItem = obj.AddComponent<EnemyHpItem>();
            hpItem.InitData(enemy);
        }

        public void RefershLife()
        {
            mLifeText.text = $"剩余生命值：{BattleManager.Homelife}";
        }

        public void RefershWaveTime(int time)
        {
            mWaveTimeText.text = time.ToString();
            if (time <= 0)
            {
                mWaveTimeText.gameObject.SetActive(false);
            }
            else
            {
                mWaveTimeText.gameObject.SetActive(true);
            }
        }

        public void RefershWave(int wave)
        {
            mWaveText.text = $"第{wave}波";
        }

        private void CreateTower(Vector3 pos, GameObject obj)
        {
            int index = mTowerButtonList.IndexOf(obj);
            
            BattleManager.CreatTower(pos,mTowerInfoList[index]);
            
        }

        protected override void OnButtonClick(string name, GameObject obj)
        {
            switch (name)
            {
                case "TowerBtn":
                {
                    Vector3 pos=mObjTowerContent.transform.localPosition;
                    RectTransform rect = GetComponent<RectTransform>();
                    pos.x/=rect.rect.width;
                    pos.y/=rect.rect.height;
                    pos.x += 0.5f;
                    pos.y += 0.5f;
                    Ray ray= Camera.main.ViewportPointToRay(pos);
                    RaycastHit[] hits = Physics.RaycastAll(ray);
                    for (int i = 0; i < hits.Length; i++)
                    {
                        if (hits[i].collider.name == "map_show")
                        {
                            pos=hits[i].point;
                            pos.x = Mathf.RoundToInt(pos.x);
                            pos.z = Mathf.RoundToInt(pos.z);
                            CreateTower(pos,obj);
                            mObjTowerContent.SetActive(false);
                        }

                    }
                }
                   
                   break;
                   }
        }

        protected override void OnNodeAsset(string name, GameObject obj)
        {
            switch (name)
            {
                case "LifeText":
                    mLifeText = obj.GetComponent<Text>();
                    break;
                case "TowerInfo":
                    mObjTowerContent = obj;
                    break;
                case "EnemyHpInfo":
                    obj.SetActive(false);
                    mEnemyHpInfo = obj;
                    break;
                case "WaveText":
                    mWaveText=obj.GetComponent<Text>();
                    break;
                case "WaveTimeText":
                    mWaveTimeText=obj.GetComponent<Text>();
                    break;
                case "TowerBtn":
                    obj.SetActive(false);
                    mBaseTowerButton = obj;
                    break;
            }
        }
    }
}

